<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * 数据库模块初始化函数文件
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id $
 * @package database
 */

/**
 * 数据库模块初始化函数
 *
 * @package	database
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id: $
 */
function &DB($params = '', $active_record = false)
{
	// Load the DB config file if a DSN string wasn't passed
	if (is_string($params) AND strpos($params, '://') === false)
	{
		include(APPPATH.'config/database'.EXT);
		$group = ($params == '') ? $active_group : $params;
		if ( ! isset($db[$group]))
		{
			throw new TDbException('db_invalid_connection_group',$group);
		}
		$params = $db[$group];
	}
	// No DB specified yet?  Beat them senseless...
	if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
	{
		throw new TDbException('db_invalid_dbdriver');
	}
	if ($active_record == true)
	{
		$params['active_r'] = true;
	}
	require_once(BASEPATH.'database/DB_driver'.EXT);
	if ( ! isset($params['active_r']) OR $params['active_r'] == true)
	{
		require_once(BASEPATH.'database/DB_active_rec'.EXT);

		if ( ! class_exists('DB'))
		{
			eval('class DB extends DB_active_record { }');
		}
	}
	else
	{
		if (!class_exists('DB'))
		{
			eval('class DB extends DB_driver { }');
		}
	}
	if(!require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT))
	{
		throw new TDbException('db_invalid_dbdriver',APPPATH,EXT);
	}
	$driver = 'DB_'.$params['dbdriver'].'_driver';
	$DB 	=& new $driver($params);
	return $DB;
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Nov 07 23:52:29 CST 2007
+--------------------------------------------------------------------------
*/
?>